package com.xunshibao.action.user;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xunshibao.service.user.UserDRO;
import com.xunshibao.service.user.UserService;
import com.xunshibao.util.SessionKey;
import com.xunshibao.web.SessionUser;

@Controller
@RequestMapping(value = "/user")
public class UserController {
	private UserService userService;

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@RequestMapping(value = "/doregister")
	public String register(@RequestParam(value = "u", required = true) String username, @RequestParam(value = "p", required = true) String password,
			@RequestParam(value = "e", required = true) String email, Model model) {
		boolean regResult = userService.register(username, password, email);
		if (regResult) {
			model.addAttribute("username", username);
		} else {
			model.addAttribute("result", false);
		}
		return "user/regok";
	}

	@RequestMapping(value = "/dologin")
	public @ResponseBody
	Model login(@RequestParam(value = "u", required = true) String username, @RequestParam(value = "p", required = true) String password,
			HttpServletRequest request, HttpServletResponse response, Model model) {
		UserDRO dro = userService.login(username, password);
		String url = request.getParameter("url");
		if (url == null || "".equals(url)) {
			url = request.getContextPath() + "/";
		}
		model.addAttribute("url", url);
		if (dro.isValid()) {
			HttpSession session = request.getSession();
			if (session != null) {
				SessionUser u = new SessionUser();
				u.setAdmin(true);
				u.setUserId(dro.getLogin().getUid());
				session.setAttribute(SessionUser.KEY, u);
				session.setAttribute(SessionKey.USER_KEY, dro.getLogin());
				model.addAttribute("state", 1);
				model.addAttribute("msg", "登录成功");
				model.addAttribute("cookie", dro.getLogin().getUsername() + "|" + dro.getLogin().getUid() + "|" + dro.getUser().getNewpm());
			}
		} else {
			model.addAttribute("msg", "登录失败，用户名不存在或密码错误");
			model.addAttribute("result", 3);
		}
		return model;
	}

	@RequestMapping(value = "/validid")
	public @ResponseBody
	Model validUsername(@RequestParam(value = "val", required = true) String username, Model model) {
		boolean userExists = userService.exists(username);
		if (userExists) {
			model.addAttribute("state", 2);
		} else {
			model.addAttribute("state", 1);
		}
		return model;
	}

	@RequestMapping(value = "/logout")
	public String logout(HttpServletRequest request, HttpServletResponse resp) {
		HttpSession session = request.getSession();
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie c : cookies) {
				c.setPath("/");
				c.setValue(null);
				c.setMaxAge(0);
				resp.addCookie(c);
			}
		}
		session.invalidate();
		return "redirect:/";
	}
}
